﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text;

namespace Core.WeiXin
{
    public static class SecretHelper
    {
        public static string SHA1_Encrypt( string Source_String )
        {
            byte[] StrRes = Encoding.Default.GetBytes( Source_String );
            HashAlgorithm iSHA = new SHA1CryptoServiceProvider();
            StrRes = iSHA.ComputeHash( StrRes );
            StringBuilder EnText = new StringBuilder();
            foreach( byte iByte in StrRes )
            {
                EnText.AppendFormat( "{0:x2}" , iByte );
            }
            return EnText.ToString();
        }

        #region 获取由SHA1加密的字符串
        public static string EncryptToSHA1( string str )
        {
            SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
            byte[] str1 = Encoding.UTF8.GetBytes( str );
            byte[] str2 = sha1.ComputeHash( str1 );
            sha1.Clear();
            ( sha1 as IDisposable ).Dispose();
            return Convert.ToBase64String( str2 );
        }
        #endregion
        #region 获取由MD5加密的字符串
        public static string EncryptToMD5( string str )
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] str1 = Encoding.UTF8.GetBytes( str );
            byte[] str2 = md5.ComputeHash( str1 , 0 , str1.Length );
            md5.Clear();
            ( md5 as IDisposable ).Dispose();
            return Convert.ToBase64String( str2 );
        }
        #endregion
    }

}
